package com.scpii.universal.util;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;

import com.scpii.universal.constant.GenericConstants;

public class EhCacheListener implements ServletContextListener {
	
	private static Log log = LogFactory.getLog(EhCacheListener.class);
	private static final String ERROR = "初始化EhCacheListener错误";
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		ResourceBundle bundle = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault());
		String cronFlag = bundle.getString(GenericConstants.CRONTAB_FLAG_ON);
		String imgUrl = bundle.getString(GenericConstants.SERVER_URL_PREFIX);
		String dispatchUrl = bundle.getString(GenericConstants.DISPATCH_SERVER_URL);
		String copyFlag = bundle.getString(GenericConstants.COPY_HOUSE_SYSCATELOG);
		String registPageShow = bundle.getString(GenericConstants.REGISTER_PAGE_SHOW);
		String messagePushModuleShow = bundle.getString(GenericConstants.MESSAGE_PUSH_MODULE_SHOW);
		String shareModuleShow = bundle.getString(GenericConstants.SHARE_MODULE_SHOW);
		String favoriteModuleShow = bundle.getString(GenericConstants.FAVORITE_MODULE_SHOW);
		String userManageModuleShow = bundle.getString(GenericConstants.USER_MANAGE_MODULE_SHOW);
		CacheManager manager;
		try {
			manager = CacheManager.create(new ClassPathResource("ehcache.xml").getInputStream());
			Cache cache = manager.getCache("propertiesCache");
			//定时任务开关
			Element ele1 = new Element(GenericConstants.CRONTAB_FLAG_ON, cronFlag);
			cache.put(ele1);
			//服务器URL前缀
			Element ele2 = new Element(GenericConstants.SERVER_URL_PREFIX, imgUrl);
			cache.put(ele2);
			//中间层URL
			Element ele3 = new Element(GenericConstants.DISPATCH_SERVER_URL, dispatchUrl);
			cache.put(ele3);
			//是否复制系统分类[房产聚合]
			Element ele4 = new Element(GenericConstants.COPY_HOUSE_SYSCATELOG, copyFlag);
			cache.put(ele4);
			//是否显示注册页面
			Element ele5 = new Element(GenericConstants.REGISTER_PAGE_SHOW, registPageShow);
			cache.put(ele5);
			Element ele6 = new Element(GenericConstants.MESSAGE_PUSH_MODULE_SHOW, messagePushModuleShow);
			cache.put(ele6);
			Element ele7 = new Element(GenericConstants.SHARE_MODULE_SHOW, shareModuleShow);
			cache.put(ele7);
			Element ele8 = new Element(GenericConstants.FAVORITE_MODULE_SHOW, favoriteModuleShow);
			cache.put(ele8);
			Element ele9 = new Element(GenericConstants.USER_MANAGE_MODULE_SHOW, userManageModuleShow);
			cache.put(ele9);
		} catch (CacheException e) {
			log.error(ERROR,e);
		} catch (IOException e) {
			log.error(ERROR,e);
		}
	}

}
